package com.victor.modular.system.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.victor.common.annotion.Permission;
import com.victor.common.annotion.log.BussinessLog;
import com.victor.common.constant.Dict;
import com.victor.common.constant.factory.ConstantFactory;
import com.victor.common.constant.tips.Tip;
import com.victor.common.controller.BaseController;
import com.victor.common.exception.BizExceptionEnum;
import com.victor.common.exception.BussinessException;
import com.victor.common.export.dictmap.AudienceExport;
import com.victor.common.persistence.dao.AudienceCodeMapper;
import com.victor.common.persistence.dao.AudienceMapper;
import com.victor.common.persistence.model.Audience;
import com.victor.common.persistence.model.AudienceCode;
import com.victor.core.log.LogObjectHolder;
import com.victor.core.support.BeanKit;
import com.victor.core.util.ExcelReport;
import com.victor.core.util.ToolUtil;
import com.victor.modular.system.dao.AudienceCodeDao;
import com.victor.modular.system.dao.AudienceDao;
import com.victor.modular.system.service.IAudienceCodeService;
import com.victor.modular.system.service.IAudienceService;
import com.victor.modular.system.transfer.AudienceCodeDto;
import com.victor.modular.system.transfer.AudienceDto;
import com.victor.modular.system.transfer.FileDto;
import com.victor.modular.system.warpper.AudienceWarpper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.File;
import java.io.IOException;
import java.util.*;

@RequestMapping("/audienceCode")
@Controller
public class AudienceCodeController extends BaseController {
    private static String PREFIX = "/system/audienceCode/";
    @Autowired
    private AudienceCodeDao audienceCodeDao;
    @Autowired
    private IAudienceCodeService audienceCodeService;
    @Autowired
    private AudienceCodeMapper audienceCodeMapper;
    /**
     * 跳转到 观众列表页面
     */
    @RequestMapping("")
    public String index(Model model) {
        return PREFIX + "weddingDress.html";
    }



    /**
     * 查询编码列表
     */
    @RequestMapping("/list")
    @Permission
    @ResponseBody
    public Object list(AudienceCodeDto params) {
        List<Map<String, Object>> list = audienceCodeDao.list(params);
        return list;
    }
    /**
     * 跳转到修改
     */
    @RequestMapping("/audienceCode_update/{id}")
    public String toEdit(@PathVariable Integer id, Model model) {
        AudienceCode audienceCode = audienceCodeMapper.selectById(id);
        Map<String, Object> menuMap = BeanKit.beanToMap(audienceCode);
        model.addAttribute("audienceCode",menuMap);
        return PREFIX + "weddingDress_edit.html";
    }
    /**
     * 跳转到新增
     */
    @RequestMapping("/audienceCode_add")
    public String toAdd(Model model) {
        return PREFIX + "weddingDress_add.html";
    }
    /**
     * 修改
     * @return
     */
    @RequestMapping(value = "/edit")
    @BussinessLog(value = "修改观众编码", key = "code", dict = Dict.AUDIENCECODE_DICT)
    @Permission
    @ResponseBody
    public Tip update(AudienceCodeDto params) throws Exception {
        if(ToolUtil.isEmpty(params)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        LogObjectHolder.me().set(audienceCodeMapper.selectById(params.getId()));
        try {
            audienceCodeService.updateAudienceCode(params);
        }catch (DuplicateKeyException e){
            throw new BussinessException(BizExceptionEnum.EXISTED_THE_AUDIENCE);
        }
            return SUCCESS_TIP;

    }

    /**
     * 新增
     * @return
     */
    @RequestMapping(value = "/add")
    @BussinessLog(value = "新增观众编码", key = "code", dict = Dict.AUDIENCECODE_DICT)
    @Permission
    @ResponseBody
    public Tip add(AudienceCodeDto params) throws Exception{
        List<AudienceCode> list = audienceCodeMapper.selectList(new EntityWrapper<AudienceCode>().eq("code", params.getCode()));
        if(list.size()>0){
            throw new BussinessException(BizExceptionEnum.EXISTED_THE_AUDIENCE);
        }else {
            audienceCodeService.addAudienceCode(params);
            return SUCCESS_TIP;
        }
    }

    /**
     * 删除
     */
    @RequestMapping(value = "/delete/{id}")
    @BussinessLog(value = "删除观众编码", key = "code", dict = Dict.AUDIENCECODE_DICT)
    @Permission
    @ResponseBody
    public Tip delete(@PathVariable Integer id) throws Exception {
        if(ToolUtil.isEmpty(id)){
            throw new BussinessException(BizExceptionEnum.REQUEST_NULL);
        }
        audienceCodeService.deleteAudienceCode(id);
        return SUCCESS_TIP;
    }


}
